﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour
{
    private static GameManager _instance;

    public static GameManager Instance
    {
        get { return _instance; }
    }

    private void Awake()
    {
        _instance = this;
        InitWidget();
    }

    /// <summary>
    /// 获取组件函数.
    /// </summary>
    private void InitWidget()
    {
        _ballPrefab = Resources.Load<GameObject>("Ball");
    }

    private int _playerScore;
    private int _aiScore;

    /// <summary>
    /// 一局游戏结束.
    /// </summary>
    /// <param name="loser">输的一方</param>
    public void GameFinished(Board.Belong loser)
    {
        switch (loser)
        {
            case Board.Belong.Player:
                _aiScore++;
                break;
            case Board.Belong.AI:
                _playerScore++;
                break;
        }

        Debug.Log("现在玩家分数为:" + _playerScore + ",AI分数为:" + _aiScore);
        GameUI.Instance.UpdateScoreText(_playerScore, _aiScore);
        JudgeWin();
        GameUI.Instance.ShowServeButton();
        _isGaming = false;
    }

    /// <summary>
    /// 判断胜利条件.
    /// </summary>
    private void JudgeWin()
    {

    }

    /// <summary>
    /// 乒乓球的预制体.
    /// </summary>
    private GameObject _ballPrefab;

    private bool _isGaming;
    /// <summary>
    /// 正在游戏中.
    /// </summary>
    public bool IsGaming
    {
        get { return _isGaming; }
    }

    /// <summary>
    /// 开始游戏.
    /// </summary>
    public void StartGame()
    {
        Debug.Log("开始游戏");
        GameObject clone = Instantiate(_ballPrefab);
        AIController.Instance.SetBall(clone);
        _isGaming = true;
    }
}
